﻿using MediatR;
using U.B.DoMain;
using U.B.ErrorCode;
using U.B.Repistory;
using U.B.WriteApi.Command;

namespace U.B.WriteApi.CommandHander
{
    public class DeluserCommandHander : IRequestHandler<DeluserCommand, ApiResult<object>>
    {
        private readonly IRepistory<User> _userRepository;

        public DeluserCommandHander(IRepistory<User> userRepository)
        {
            _userRepository = userRepository;
        }
        /// <summary>
        /// 用户删除
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ApiResult<object>> Handle(DeluserCommand request, CancellationToken cancellationToken)
        {
            ApiResult<object> apiResult = new ApiResult<object>();
            var result = _userRepository.GetAll().FirstOrDefault(x => x.UserId == request.UserId);
            result.IsDeleted = true;
            if (result != null)
            {
                var result1 = _userRepository.UpdateAsync(result).Result;
                if (result1 > 0)
                {
                    apiResult.code = Renum.删除成功;
                    apiResult.Message = Renum.删除成功.ToString();
                    apiResult.Data = result1;
                }
                else
                {
                    apiResult.code = Renum.删除失败;
                    apiResult.Message = Renum.删除失败.ToString();
                    apiResult.Data = result1;
                }
            }
            return await Task.FromResult(apiResult);

        }

    }
}
